const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

const EvaluationRecord = sequelize.define('evaluation_record', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "评测记录ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  text: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: "评测文本"
  },
  audio_url: {
    type: DataTypes.STRING(255),
    allowNull: false,
    comment: "音频文件路径"
  },
  score: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "总分"
  },
  accuracy: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "准确度"
  },
  fluency: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "流利度"
  },
  completeness: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "完整度"
  },
  level: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: "评级"
  },
  suggestions: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "改进建议"
  },
  duration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "录音时长(秒)"
  },
  evaluation_type: {
    type: DataTypes.ENUM('free', 'course', 'test'),
    defaultValue: 'free',
    comment: "评测类型"
  }
}, {
  tableName: 'evaluation_records',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['evaluation_type']
    },
    {
      fields: ['created_at']
    }
  ]
});

const GameRecord = sequelize.define('game_record', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "游戏记录ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  content_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "游戏内容ID"
  },
  content_title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "内容标题"
  },
  user_pronunciation: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "用户发音"
  },
  standard_pronunciation: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "标准发音"
  },
  similarity: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "相似度"
  },
  score: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "得分"
  },
  level: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: "评级"
  },
  suggestions: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "建议"
  },
  audio_url: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: "音频文件路径"
  }
}, {
  tableName: 'game_records',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['content_id']
    },
    {
      fields: ['created_at']
    }
  ]
});

const DailyRecord = sequelize.define('daily_record', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "每日记录ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  sentence_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "句子ID"
  },
  sentence: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: "句子内容"
  },
  date: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    comment: "日期"
  },
  score: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "得分"
  },
  level: {
    type: DataTypes.STRING(20),
    allowNull: false,
    comment: "评级"
  },
  accuracy: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "准确度"
  },
  fluency: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "流利度"
  },
  completeness: {
    type: DataTypes.DECIMAL(5, 2),
    allowNull: false,
    comment: "完整度"
  },
  video_url: {
    type: DataTypes.STRING(255),
    allowNull: true,
    comment: "视频文件路径"
  },
  suggestions: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "建议"
  }
}, {
  tableName: 'daily_records',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['date']
    },
    {
      unique: true,
      fields: ['user_id', 'date']
    }
  ]
});

module.exports = {
  EvaluationRecord,
  GameRecord,
  DailyRecord
};
